package com.app.micaihu.view.main.game.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.bean.game.GameCenterEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.components.downandupload.LoadingView;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.custom.view.RatingBarView;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.utils.util.StringUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;
import com.facebook.drawee.generic.RoundingParams;

import java.util.List;

/**
 * Created by Guo on 2017/3/23.
 * <p>
 * 游戏排行榜
 */

public class GameRankApdater extends AppBaseAdapter<GameCenterEntity> {


    public GameRankApdater(List<GameCenterEntity> list, Context context) {
        super(list, context);
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_game_rank, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MobclickAgentUtils.event(UmEventId.game_exposed, UmEventId.INSTANCE.getGamecenter());
        StatService.onEvent(context, "game_exposed", "排行榜", 1);//统计事件的点击次数
        GameCenterEntity gameCenterEntity = list.get(position);
        if (gameCenterEntity == null) {
            return convertView;
        }

        LoadImageUtils.getInstance().loadImage(viewHolder.headView, gameCenterEntity.getAppIcon());


        viewHolder.titleView.setText(gameCenterEntity.getArticleTitle());
        viewHolder.descView.setText(gameCenterEntity.getSlogan());


//        viewHolder.gameSize.setText(gameCenterEntity.getAppSize());

        viewHolder.start.setStar(StringUtils.toFloat(gameCenterEntity.getGradeScore(), 0.0f) / 2);
        viewHolder.gameScore.setText(gameCenterEntity.getGradeScore());
        viewHolder.gameType.setText(gameCenterEntity.getGameType());
        if (TextUtils.isEmpty(gameCenterEntity.getGameType())) {
            viewHolder.verticalLine.setVisibility(View.GONE);
        } else {
            viewHolder.verticalLine.setVisibility(View.VISIBLE);
        }


        viewHolder.tvRankNum.setVisibility(View.INVISIBLE);
        viewHolder.ivRankNum.setVisibility(View.VISIBLE);
        if (position == 0) {
            viewHolder.ivRankNum.setImageResource(R.drawable.game_ranking_a);
        } else if (position == 1) {
            viewHolder.ivRankNum.setImageResource(R.drawable.game_ranking_b);
        } else if (position == 2) {
            viewHolder.ivRankNum.setImageResource(R.drawable.game_ranking_c);
        } else {
            viewHolder.tvRankNum.setVisibility(View.VISIBLE);
            viewHolder.ivRankNum.setVisibility(View.INVISIBLE);
            viewHolder.tvRankNum.setText((position + 1) + "");
        }
        viewHolder.downloadView.setTag(gameCenterEntity);
        return convertView;
    }


    private class ViewHolder {
        CustomImageView headView;
        TextView titleView;
        TextView descView;
        //        TextView gameSize;
        LoadingView downloadView;
        TextView tvRankNum;
        ImageView ivRankNum;

        RatingBarView start;
        TextView gameScore;
        TextView gameType;
        View verticalLine;

        public ViewHolder(View contentView) {
            headView = (CustomImageView) contentView.findViewById(R.id.game_logo);
            titleView = (TextView) contentView.findViewById(R.id.game_title);
            descView = (TextView) contentView.findViewById(R.id.game_desc);
//            gameSize = (TextView)contentView.findViewById(R.id.game_size);

            downloadView = (LoadingView) contentView.findViewById(R.id.game_download);
            tvRankNum = (TextView) contentView.findViewById(R.id.tv_rank_num);
            gameScore = (TextView) contentView.findViewById(R.id.game_score);
            gameType = (TextView) contentView.findViewById(R.id.game_type);
            ivRankNum = (ImageView) contentView.findViewById(R.id.iv_rank_num);
            start = (RatingBarView) contentView.findViewById(R.id.rb_score);
            verticalLine = contentView.findViewById(R.id.vertical_line);
            headView.getHierarchy().setRoundingParams(new RoundingParams().setCornersRadius(28.0f));

        }
    }
}
